// pages/user/adress/addNewAddress.js
import {getRegionTree, addAddress} from '../../../apis/index'
import { areaList } from './citylist'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phone: '',
    detail: '',
    area: '',
    showAreaSheet: false,
    provinceList: [],
    cityList: [],
    areaList: [],
    pickerValue: [],
    pickerCity: [],
    pickerArea: [],
    region: [],
  },
  showArea(){
    console.log("地区选择")
    this.setData({
      showAreaSheet: true
    })
  },
  confirm(ev){
    console.log(ev.detail)
    const values = ev.detail.values
    const area = values[0].name+',' + values[1].name +','+ values[2].name

    /*
      {"name":"朱蒙托","phone":"18822898732","region":[{"label":"天津","value":19},{"label":"天津市","value":20},{"label":"河东区","value":22}],"detail":"单独发发阿斯蒂芬"}
    */ 
    this.setData({
      showAreaSheet: false,
      area: area
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData()
  },
  async initData(){
    const res = await getRegionTree()
    // 将list对象转换成数组
    let list = res.data.list;
    let province_list = []
    for(let key in list){
      province_list.push(list[key])
    }
    let city_list = []
    for(let key in res.data.list[1].city){
        city_list.push(res.data.list[1].city[key])
    }
    let area_list = []
    for(let key in res.data.list[1].city[2].region){
      area_list.push(res.data.list[1].city[2].region[key])
    }
    console.log('province_list', province_list)
    let region = this.data.region
    region[0] = {"value":province_list[0].id,"label":province_list[0].name}
    region[1] = {"value": city_list[0].id, "label": city_list[0].name}
    region[2] = {"value": area_list[0].id, "label": area_list[0].name}
    let area = region[0].label +  region[1].label +  region[2].label
    this.setData({
      provinceList: province_list,
      cityList:city_list,
      areaList: area_list,
      region: region
    })
  },
  // 选择省份
  pickProvinceEnd(ev){

  },
  changeProvince(ev){
    let index = ev.detail.value[0]
    // 获取省份
    let province = this.data.provinceList[index]
    // 联动改变 市,区
    let citys = province['city'];
    let city_list = []
    for(let key in citys){
      city_list.push(citys[key])
    }
    // 联动改变城区

    let cityRegion = city_list[0].region
    let area_list = []
    for(let key in cityRegion){
      area_list.push(cityRegion[key])
    }

    let region = this.data.region
    region[0] = {"value":province.id,"label":province.name}
    region[1] = {"value": city_list[0].id, "label": city_list[0].name}
    region[2] = {"value": area_list[0].id, "label": area_list[0].name}
    console.log('region----', region)
    this.setData({
      cityList: city_list,
      areaList: area_list,
      region: region
    })
  },
  changeCity(ev){
    let cityIndex = ev.detail.value[0]
    let city = this.data.cityList[cityIndex];
    let cityRegion = this.data.cityList[cityIndex].region
    let area_list = []
    for(let key in cityRegion){
      area_list.push(cityRegion[key])
    }
    console.log(area_list)
    let region = this.data.region
    region[1] = {"value": city.id, "label": city.name}
    region[2] = {"value": area_list[0].id, "label": area_list[0].name}
    console.log(region)
    this.setData({
      areaList: area_list,
      region: region
    })

  },
  changeArea(ev){
    let areaIndex = ev.detail.value[0]
    let selectedArea = this.data.areaList[areaIndex]
    let region = this.data.region
    region[2] = {"value": selectedArea.id, "label": selectedArea.name}
    this.setData({
      region: region
    })
    console.log(this.data.region)
  },
  confirm(){
    console.log(this.data.region)
    let region = this.data.region
    let area = region[0].label+"/"+region[1].label+"/"+region[2].label
    this.setData({
      area: area
    })
    this.close()
  },
  close(){
    this.setData({
      showAreaSheet: false
    })
  },
  // 提交注册
  async submit(){
    let {name,phone,detail,region} = this.data
    console.log(name,phone,detail,region)
    let data = {
      form: {
        name:name,
        phone:phone,
        detail: detail,
        region: region
      }
    }
    console.log(data)
    const res = await addAddress({data})
    console.log("新增结果")
    if (res.status === 200) {
      wx.navigateBack({
        delta: 0,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})